package com.campus.client.controller;


import com.campus.client.net.ClientConnector;
import com.campus.common.message.LibraryResponse;
import com.campus.common.vo.Book;
import com.campus.common.vo.BorrowRecord;

import java.io.IOException;
import java.util.List;

public class LibraryController {
    private ClientConnector connector;
	
	public LibraryController(ClientConnector connector) {
        this.connector = connector;
    }
	
	public List<Book> getBooks() throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.listBooks();
        return resp.getBooks();
    }
    public List<Book> getUserBorrowedBooks(String username) throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.getUserBorrowedBooks(username);
        return resp.getBooks();
    }
    public List<BorrowRecord> getBookBorrowers(int bookId) throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.getBookBorrowers(bookId);
        return resp.getBorrowRecords();
    }
    public String borrowBook(int id, String username) throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.borrowBook(id, username);
        return resp.getMessage();
    }
    public List<BorrowRecord> getUserBorrowRecords(String username) throws ClassNotFoundException, IOException {
        System.out.println("DEBUG: 正在向服务器请求用户借阅记录，用户名: " + username);
        LibraryResponse resp = connector.getUserBorrowRecords(username);
        System.out.println("DEBUG: 服务器响应 - 成功: " + resp.isSuccess() + ", 消息: " + resp.getMessage());
        System.out.println("DEBUG: 借阅记录数量: " + (resp.getBorrowRecords() != null ? resp.getBorrowRecords().size() : "null"));
        return resp.getBorrowRecords();
    }
    public String returnBook(int id, String username) throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.returnBook(id, username);
        return resp.getMessage();
    }
    public String addBook(Book book) throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.addBook(book);
        return resp.getMessage();
    }
    public String deleteBook(int id) throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.deleteBook(id);
        return resp.getMessage();
    }
    public List<Book> searchBooksByTitle(String title) throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.searchBooksByTitle(title);
        return resp.getBooks();
    }

    public List<Book> searchBooksByAuthor(String author) throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.searchBooksByAuthor(author);
        return resp.getBooks();
    }

    public List<Book> searchBooks(String title, String author) throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.searchBooks(title, author);
        return resp.getBooks();
    }
    public String updateBook(Book book) throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.updateBook(book);
        return resp.getMessage();
    }
    // 新增按类别搜索方法
    public List<Book> searchBooksByCategory(String category) throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.searchBooksByCategory(category);
        return resp.getBooks();
    }

    // 新增获取所有类别方法
    public List<String> getAllCategories() throws ClassNotFoundException, IOException {
        LibraryResponse resp = connector.getAllCategories();
        return resp.getBorrowers();  // 借用borrowers字段返回类别列表
    }
    
 // 可选：结束会话时关闭连接
    public void closeConnection() throws IOException {
        connector.close();
    }
}
